package com.kcsj.controller.user;
//201902104020 李鑫元
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.user.Role;
import com.kcsj.service.user.RoleService;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;


@Slf4j
@WebServlet("/role")
public class RoleController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //根据request对象，获得代表参数的JSON字串
        String role_json = JSONUtil.getJSON(request);
        try {
            //将JSON字串解析为student对象
            Role role = JSON.parseObject(role_json, Role.class);
            RoleService.getInstance().add(role);
            MessageHelper.success();
            JSONObject message = MessageHelper.addMessage("增加成功");
            response.getWriter().println(message);
            log.info("增加成功");
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        String id = request.getParameter("id");
        try {
            if(id!=null){
                this.responseRole(Integer.parseInt(id), response);
            }else {
                this.responseRoles(response);
            }
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            response.getWriter().println(message);
        }catch(Exception e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            response.getWriter().println(message);
        }
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = JSONUtil.getJSON(request);
        try {
            Role role = JSON.parseObject(json,Role.class);
            //到数据库表修改user对象对应的记录
            RoleService.getInstance().update(role);
            JSONObject message = MessageHelper.addMessage("修改成功");
            response.getWriter().println(message);
        } catch (SQLException e) {
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            response.getWriter().println(message);
        } catch (Exception e) {
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }

    private void responseRole(int id, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //根据id查找角色
        Role role = RoleService.getInstance().find(id);
        String role_json = JSON.toJSONString(role);
        JSONObject data = MessageHelper.addData(role_json);
        //响应role到前端
        response.getWriter().println(data);
    }

    //按查询条件响应角色对象
    private void responseRoles(HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //获得所有角色
        Collection<Role> students = RoleService.getInstance().findAll();
        String students_json = JSON.toJSONString(students, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(students_json);
        //响应role到前端
        response.getWriter().println(data);
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        try {
            if (!id.equals("")) {
                //到数据库表中删除对应的学位
                RoleService.getInstance().delete(Integer.parseInt(id));
                JSONObject message = MessageHelper.addMessage("删除成功");
                log.info("role{}删除成功",id);
                response.getWriter().println(message);
            }
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.addMessage("数据库操作异常");
            e.printStackTrace();
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.addMessage("网络异常");
            e.printStackTrace();
            response.getWriter().println(message);
        }
    }

}
